﻿using Pb;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

public class DDZHall : Game {
    public GameObject Root;
    EginUser user = EginUser.Instance;
    EginDDZ ddz = EginDDZ.Instance;
    public UILabel UserName;
    public UILabel UID;
    public UILabel CoinCount;
    public UISprite avatarSprit;
    public GameObject ReturnBtn;

    private AsyncOperation async;
    private uint _nowprocess;
    private void Awake()
    {
        UIEventListener.Get(ReturnBtn).onClick = ButtonClick;
        base.StartGameSocket();
        SocketManager.Instance.socketListener = this;
    }
    private float TimeOne;
    private float TimeTwo;
    private void Start()
    {
        _nowprocess = 0;
        UserName.text = user.nickname;
        UID.text = user.Uid;
        string str = user.ReturnRelCoin(int.Parse(user.coinCount));
        CoinCount.text = str;
        avatarSprit.spriteName = user.avatarId.ToString();
        if (user.ControlScene == 1)
        {
            TimeOne = Time.realtimeSinceStartup;
            GameObject FriendRoomPanel = NGUITools.AddChild(Root, (GameObject)(Resources.Load(GameDDZPrafabPath.FriendRoomPanel)));
            TimeTwo = Time.realtimeSinceStartup;
            float time = TimeTwo - TimeOne;
            Debug.Log("加载的时间为" + time);
            
        }
        else
        {
            GameObject DDZCoinRoom = NGUITools.AddChild(Root, (GameObject)(Resources.Load(GameDDZPrafabPath.DDZCoinRoom)));
            //GameObject FriendRoomPanel = NGUITools.AddChild(Root, (GameObject)(Resources.Load(GameDNPrafabPath.CoinRoom)));
        }
    }
    void ButtonClick(GameObject button)
    {
        if (button.name == "ReturnBtn")
        {
            Application.LoadLevel(ScenePath.Hall);
        }

    }
    /// <summary>
    /// 监听服务器消息
    /// </summary>
    /// <param name="data"></param>
    public override void SocketReceiveMessage(byte[] data)
    {
        base.SocketReceiveMessage(data);
        byte[] dataLength = data.Skip(0).Take(2).ToArray();
        byte[] dataId = data.Skip(2).Take(4).ToArray();
        byte[] dataConcent = data.Skip(data.Length - (data.Length - 4)).ToArray();
        int TempMsgID = ProtoDataToMsg.Byte2ushort(dataId);
        //斗地主创建房间返回
        if (TempMsgID == MsgId.DdzOpenRoomResponse)
        {
            Pb.DdzOpenRoomResponse newpb = Pb.DdzOpenRoomResponse.Parser.ParseFrom(dataConcent);
            Debug.Log("消息ID" + TempMsgID + "消息内容" + newpb);
            if (newpb.Status == STATUS.Success)
            {
                ddz.createrUid = newpb.RoomInfo.CreaterUid;
                ddz.roomId = newpb.RoomInfo.RoomId;
                ddz.totalRound = newpb.RoomInfo.TotalRound;
                ddz.seatId = newpb.SeatId;
                ddz.roomStatus = newpb.RoomInfo.RoomStatus;
                Debug.Log(ddz.seatId + "hall");
                user.gameUserInfoList.Add(newpb.UserInfo);
                StartCoroutine(StartGameDDZ());
                
            }
            else
            {
                user.logMsg = newpb.Msg;
                GameObject Set = NGUITools.AddChild(Root, (GameObject)(Resources.Load("LogMsg")));
            }
        }
        //斗地主根据ID加入房间返回
        else if (TempMsgID == MsgId.DdzEnterRoomResponse)
        {
            Pb.DdzEnterRoomResponse newpb = Pb.DdzEnterRoomResponse.Parser.ParseFrom(dataConcent);
            Debug.Log("消息ID" + TempMsgID + "消息内容" + newpb);
            if (newpb.Status == STATUS.Success)
            {
                ddz.createrUid = newpb.RoomInfo.CreaterUid;
                ddz.roomId = newpb.RoomInfo.RoomId;
                ddz.totalRound = newpb.RoomInfo.TotalRound;
                ddz.seatId = newpb.SeatId;
                ddz.roomStatus = newpb.RoomInfo.RoomStatus;
                user.gameUserInfoList = newpb.UserInfoList.ToList();
                StartCoroutine(StartGameDDZ());
            }
            else
            {
                user.logMsg = newpb.Msg;
                GameObject Set = NGUITools.AddChild(Root, (GameObject)(Resources.Load("LogMsg")));
            }
        }
        //斗地主随机进入房间返回
        else if (TempMsgID == MsgId.DdzRandomEnterRoomResponse)
        {
            Pb.DdzRandomEnterRoomResponse newpb = Pb.DdzRandomEnterRoomResponse.Parser.ParseFrom(dataConcent);
            Debug.Log("消息ID" + TempMsgID + "消息内容" + newpb);
            if (newpb.Status == STATUS.Success)
            {
                ddz.createrUid = newpb.RoomInfo.CreaterUid;
                ddz.roomId = newpb.RoomInfo.RoomId;
                ddz.totalRound = newpb.RoomInfo.TotalRound;
                ddz.seatId = newpb.SeatId;
                ddz.roomStatus = newpb.RoomInfo.RoomStatus;
                user.gameUserInfoList = newpb.UserInfoList.ToList();
                StartCoroutine(StartGameDDZ());
            }
            else
            {
                user.logMsg = newpb.Msg;
                GameObject Set = NGUITools.AddChild(Root, (GameObject)(Resources.Load("LogMsg")));
            }

        }
        //网络异常
        else if (TempMsgID == MsgId.NetWorkAbnormalMessage)
        {
            GameObject NetWorkAbnormal = NGUITools.AddChild(GameObject.Find("UI Root"), (GameObject)(Resources.Load("NetWorkAbnormal")));
        }
    }

    protected IEnumerator StartGameDDZ()
    {
        GameObject LoadSceneAsync = NGUITools.AddChild(Root, (GameObject)(Resources.Load("Public/LoadSceneAsync")));
        //异步读取场景。
        //Globe.loadName 就是A场景中需要读取的C场景名称。
        //async = Application.LoadLevelAsync(ScenePath.GameDDZPlay);
        Application.LoadLevelAsync(ScenePath.GameDDZPlay);
        //async.allowSceneActivation = false;
        //读取完毕后返回， 系统会自动进入C场景
        yield return async;
    }

    void Update()
    {
        
        //if (async == null)
        //{
        //    return;
        //}
        

        //uint toProcess;
        //if (async.progress < 0.9f)//坑爹的progress，最多到0.9f
        //{
        //    toProcess = (uint)(async.progress * 100);
        //}
        //else
        //{
        //    toProcess = 100;

        //}

        //if (_nowprocess < toProcess)
        //{
        //    _nowprocess++;
        //}
        //if (_nowprocess == 100)//async.isDone应该是在场景被激活时才为true
        //{
        //    Destroy(GameObject.Find("LoadSceneAsync"));
        //    async.allowSceneActivation = true;

        //}


    }
}
